package faceb.server.service.impl;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.mysql.fabric.xmlrpc.base.Array;

import faceb.server.dao.CommentDao;
import faceb.server.dao.NewsDao;
import faceb.server.dao.NewspicDao;
import faceb.server.model.AjaxResponse;
import faceb.server.model.Comment;
import faceb.server.model.News;
import faceb.server.model.NewsPic;
import faceb.server.model.User;
import faceb.server.service.NewsService;
import faceb.server.util.ServerFinal;
import faceb.server.util.UUIDUtil;

@Service("newsService")
public class NewsServiceImpl implements NewsService {

	private Logger logger = LoggerFactory.getLogger(NewsServiceImpl.class);

	@Resource(name = "newsDao")
	private NewsDao newsDao;

	@Resource(name = "newspicDao")
	private NewspicDao newspicDao;

	@Resource(name = "commentDao")
	private CommentDao commentDao;

	private Gson gson = new Gson();

	public int insertNews(News news) {
		return newsDao.insertNews(news);
	}

	public int insertNewsPic(NewsPic newspic) {
		return newspicDao.insertNewspic(newspic);
	}

	public String publicNews(Map<String, String> params, List<File> pics,
			HttpServletRequest req) {
		AjaxResponse ar = new AjaxResponse();

		String nid = UUIDUtil.get32Code();
		String topic = params.get("topic");
		String title = params.get("title");
		String uid = ((User) req.getSession().getAttribute("user")).getU_id();
		long ntime = System.currentTimeMillis();
		String content = params.get("content");

		if (uid == null || content == null) {
			return gson.toJson(ar);
		}

		News news = new News(nid, topic, title, uid, ntime, content, 0, 0);
		newsDao.insertNews(news);
		for (File pic : pics) {
			String np_id = UUIDUtil.get32Code();
			String picTyep = pic.getName().substring(
					pic.getName().lastIndexOf("."));
			File newFilePath = Paths.get(ServerFinal.RESOURCE_DIR, uid, "pics",
					np_id + picTyep).toFile();
			try {
				FileUtils.moveFile(pic, newFilePath);
			} catch (IOException e) {
				e.printStackTrace();
			}

			String picUrl = ServerFinal.SERVER_URL+ "pics/"+np_id;
			NewsPic newpic = new NewsPic(np_id, nid, newFilePath.getName(),
					picUrl);
			newspicDao.insertNewspic(newpic);
		}

		ar.setStatus("0");
		ar.setInfo("success");
		ar.setData("");
		return gson.toJson(ar);
	}

	public Map<String, Object> getNewsByNewsId(String newsid) {

		Map<String, Object> newsObject = new HashMap<String, Object>();
		Map<String,Object> news = newsDao.getNewsByNewsId(newsid);
		if (news == null) {
			return null;
		}
		newsDao.newsViewcountAdd(newsid);   //增加用户的浏览次数
		List<Map<String,Object>> comments = commentDao.getCommentByNewsId(newsid);
		List<NewsPic> pics = newspicDao.getNewspicByNewsId(newsid);
		newsObject.put("news", news);
		newsObject.put("comments", comments);
		newsObject.put("pics", pics);
		return newsObject;
	}

	public List<Map<String, Object>> getNewsByUserId(String userid) {
		List<Map<String,Object>> newsObjects = new ArrayList<Map<String,Object>>();
		List<News> newses = newsDao.getNewsByUserId(userid);
		for (News news :newses) {
			Map<String,Object> newsObject = getNewsByNewsId(news.getN_id());
			newsObjects.add(newsObject);
		}
		return newsObjects;
	}

	public List<Map<String, Object>> getNewsBeforeTime(long time, int count) {
		List<Map<String,Object>> newsObjects = new ArrayList<Map<String,Object>>();
		List<News> newses = newsDao.getNewsBeforeTime(time, count);
		for (News news :newses) {
			Map<String,Object> newsObject = getNewsByNewsId(news.getN_id());
			newsObjects.add(newsObject);
		}
		return newsObjects;
	}

	public List<Map<String, Object>> getNewsAfterTime(long time, int count) {
		List<Map<String,Object>> newsObjects = new ArrayList<Map<String,Object>>();
		List<News> newses = newsDao.getNewsAfterTime(time, count);
		for (News news :newses) {
			Map<String,Object> newsObject = getNewsByNewsId(news.getN_id());
			newsObjects.add(newsObject);
		}
		return newsObjects;
	}

}
